﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_Asp_UserControls_TextureSelection : System.Web.UI.UserControl
{     
    protected void Page_Load(object sender, EventArgs e)
    {
        x_upload_error.Visible = false;
        x_selectionList.DataSource = ModelItemHandler.GetTexturesList();
        x_selectionList.DataBind();        
    }

    public override bool Visible
    {        
        set
        {
            base.Visible = value;
            if (value && Session[Strings.Session_CurrentTextureChange] != null)
            {
                Texture.Type f_currentType = (Texture.Type)Session[Strings.Session_CurrentTextureChange];
                x_header.InnerText = "Edit " + Texture.GetTypeString(f_currentType);
            }
        }
    }
    
    protected void selectionList_Changed(object sender, EventArgs e)
    {
        x_selectionList.DataBind();
        DataListItem f_item = x_selectionList.SelectedItem;

        //So hardcoded substringing
        x_itemInfo_filename.Text = (f_item.FindControl("x_selectedItemImage") as Image).ImageUrl.Substring(2);
    }

    protected void selectionList_ItemCommand(object source, DataListCommandEventArgs e)
    {
        DataList dl = source as DataList;

        int selIdx = dl.SelectedIndex;

        switch (e.CommandName.ToLower())
        {
            case "select":
                selIdx = e.Item.ItemIndex;
                x_itemInfoPanel.Visible = true;
                break;
            case "unselect":
                selIdx = -1;
                x_itemInfoPanel.Visible = false;
                break;
        }

        if (selIdx != dl.SelectedIndex)
            dl.SelectedIndex = selIdx;
        dl.DataBind();
    }

    protected void CloseSelection(object sender, EventArgs e)
    {
        x_selectionList.SelectedIndex = -1;        
        x_upload_error.Text = "";
        x_upload_filename.Text = "";
        this.Visible = false;          
    }

    protected void UpdateTexture(object sender, EventArgs e)
    {
        try
        {
            Dictionary<Texture.Type, string> f_changes;
            if (Session[Strings.Session_TextureChanges] == null)
            {
                f_changes = new Dictionary<Texture.Type, string>();
                Session[Strings.Session_TextureChanges] = f_changes;
            }
            else
            {
                f_changes = Session[Strings.Session_TextureChanges] as Dictionary<Texture.Type, string>;
            }           
            
            Texture.Type f_currentType = (Texture.Type)Session[Strings.Session_CurrentTextureChange];

            if (!f_changes.ContainsKey(f_currentType) )
            {
                f_changes.Add(f_currentType, x_itemInfo_filename.Text);
            }
            else
            {
                f_changes[f_currentType] = x_itemInfo_filename.Text;
            }           
            
            this.Visible = false;

            string f_id = (Request.QueryString[Strings.Query_Edit_ID] == null) ? "" : "?" + Strings.Query_Edit_ID + "=" + Request.QueryString[Strings.Query_Edit_ID];
            Response.Redirect(Request.Path + f_id);
            //Server.Transfer(Request.RawUrl);
        }
        catch (Exception ex)
        {
            
        }        
    }

    protected void TryToUpload(object sender, EventArgs e)
    {
        
        bool f_validated = true;
        //Check file extension on uploaded file and output texture, also checks that 
        if (!x_upload_file.FileName.EndsWith(".png") )
        {
            x_upload_error.Text = "File needs to be png";
            f_validated = false;
            
        }
        else if(!x_upload_filename.Text.EndsWith(".png"))
        {
            x_upload_error.Text = "Filename needs to end with .png";
            f_validated = false;
        }
        else if (x_upload_filename.Text == "")
        {
            x_upload_error.Text = "You need to specify a filename";
            f_validated = false;
        }
        else if (x_upload_filename.Text.Contains("..") || x_upload_filename.Text.StartsWith(".") || x_upload_filename.Text.StartsWith("/") || x_upload_filename.Text.StartsWith("\\"))
        {
            x_upload_error.Text = "Output filename's path is not a valid one. Make sure it doesn't contain ../ or starts with ./\\";
            f_validated = false;
        }

        if (f_validated)
        {
            if (ModelItemHandler.UploadFile(x_upload_file, x_upload_filename.Text.ToLower(), Paths.texturesFolder ) )
            {
                //If it uploads succesfully then add the file to the datalist
                DataView f_dv = x_selectionList.DataSource as DataView;
                DataRow f_dr = f_dv.Table.NewRow();

                f_dr[0] = x_upload_filename.Text.ToLower();
                f_dv.Table.Rows.Add( f_dr);                
                
                x_selectionList.DataBind();
                //Set the output text to ""
                x_upload_filename.Text = "";                
            }
            else
            {
                x_upload_error.Text = "Failed to save file";
                x_upload_error.Visible = true;
            }
        }
        else
        {
            x_upload_error.Visible = true;
        }

    }
}